//
//  MyInformationViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/9/5.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MyInformationViewController.h"
#import "MyInformationTableViewCell.h"
#import "NSString+MD5.h"

@interface MyInformationViewController ()<UITableViewDelegate , UITableViewDataSource>
{
    BOOL state;
}
Strong UITableView *tableView;
Strong UIView *headView;
Strong NSMutableArray *dataArray; /**<*/
@property (nonatomic,strong) UIImageView *imgView;

@end

//网络
@implementation MyInformationViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId,
                                 @"auMobile":self.dataArray[1][@"text"],
                                 @"password":[NSString md5:self.dataArray[2][@"text"]],
                                 @"nickname":self.dataArray[0][@"text"],
                                 @"payPassword":self.dataArray[3][@"text"],};
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/Login_Registration/modifyUserInformation.action" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *data = UIImageJPEGRepresentation(self.imgView.image, .1);
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        [formData appendPartWithFileData:data name:@"file" fileName:[NSString stringWithFormat:@"%ld.jpg",(NSUInteger)time] mimeType:@"image/jpeg"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            [UserModel sharedInstanced].headImg = [NSString stringWithFormat:@"%@",responseObject[@"obj"][@"headPortrait"]];
            [UserModel sharedInstanced].mobile = responseObject[@"obj"][@"tel"];
            [UserModel sharedInstanced].payThePassword = responseObject[@"obj"][@"payPassword"];
            [UserModel sharedInstanced].password = self.dataArray[2][@"text"];
            [UserModel sharedInstanced].nameNick = responseObject[@"obj"][@"nickname"];
            [self.view makeToast:responseObject[@"msg"] duration:1 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        [self hiddenProgressHUD];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
        [self hiddenProgressHUD];
    }];
}

@end

@implementation MyInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem.title = @"修改";
    state = NO;
    self.title = @"我的信息";
    [self.view addSubview:self.tableView];
    
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:NO animated:NO];
    [self.view endEditing:YES];
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"完成"]) {
        self.navigationItem.rightBarButtonItem.title = @"修改";
        [self getData];
        state = NO;
    }else{
        self.navigationItem.rightBarButtonItem.title = @"完成";
        state = YES;
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - click

- (void)tableHeadViewCkick{
    [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) {
        self.imgView.image = img;
    }];
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInformationTableViewCell *cell = [MyInformationTableViewCell cellForTableView:tableView];
    cell.state = state;
    cell.model = self.dataArray[indexPath.row];
    cell.getTextField = ^(NSString *str) {
        self.dataArray[indexPath.row][@"text"] = str;
        [self.tableView reloadData];
    };
    //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44 / WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - init

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 66 / WIDTH_5S_SCALE + 5 / WIDTH_5S_SCALE)];
        _headView.backgroundColor = getColor(@"eeeeee");
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 66 / WIDTH_5S_SCALE)];
        bgView.backgroundColor = getColor(@"ffffff");
        [_headView addSubview:bgView];
        UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 7 / WIDTH_5S_SCALE - 15 / WIDTH_5S_SCALE, 0, 7 / WIDTH_5S_SCALE, 66 / WIDTH_5S_SCALE)];
        rightImageView.contentMode = UIViewContentModeScaleAspectFit;
        rightImageView.image = [UIImage imageNamed:@"right_image"];
        [bgView sd_addSubviews:@[self.imgView ,  rightImageView]];
        _headView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableHeadViewCkick)];
        [_headView addGestureRecognizer:tap];
    }
    return _headView;
}

- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 10 / WIDTH_5S_SCALE, 46 / WIDTH_5S_SCALE, 46 / WIDTH_5S_SCALE)];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.backgroundColor = getColor(@"90cef8");
        [_imgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:[UserModel sharedInstanced].headImg]];
        _imgView.layer.masksToBounds = YES;
        _imgView.layer.cornerRadius = 23 / WIDTH_5S_SCALE;
    }
    return _imgView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsZero;
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [_tableView setLayoutMargins:UIEdgeInsetsZero];
        }
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
    }
    return _tableView;
}



#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[@{@"title":@"用户名:",@"image":@"user",@"text":[UserModel sharedInstanced].nameNick }.mutableCopy,
                        @{@"title":@"联系电话:",@"image":@"phone",@"text":[UserModel sharedInstanced].mobile}.mutableCopy,
                        @{@"title":@"登录密码:",@"image":@"TheLoginPassword",@"text":[UserModel sharedInstanced].password}.mutableCopy,
                        @{@"title":@"支付密码:",@"image":@"PayThePassword",@"text":[UserModel sharedInstanced].payThePassword}.mutableCopy,
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
